using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;

namespace OA.BLL
{
    public class Chat:Base
    {
        private DAL.Chat dal = null;

        public Chat()
        {
            isession = DB_help.NHibernateHelper.GetCurrentSession(DefaultDataBase);
            dal=new DAL.Chat(isession);
        }

        public Common.Result Send(string ReceivedUserName, string Message,out Model.Chat model)
        {
            model = null;
            Common.Result ret = new Common.Result(false, 0, "添加失败");
            if (new DAL.Manager(isession).GetModel(ReceivedUserName) == null)
            {
                ret.message = "接收人不存在";
                return ret;
            }
            model = new Model.Chat()
            {
                SendUserName = LoginUser.LoginManager.UserName,
                SendTime = DateTime.Now,
                ReceivedUserName = ReceivedUserName,
                Message = Message,
                IsReceived = false
            };
            ret = dal.Add(model);
            if (ret.success) isession.Refresh(model);
            return ret;
        }

        public IList<Model.Chat> Received()
        {

            string where = "it.IsReceived=false and it.ReceivedUserName=?";
            var list = dal.Get(where, "it.ID asc", 0, 0, BLL.LoginUser.LoginManager.UserName);
            return list;
        }

        public Common.Result SetIsReceived( int ID)
        {
            Common.Result ret = new Common.Result(false, 0, "");
            var model=dal.GetModel(ID);
            if (model == null)
            {
                ret.message = "数据不存在在";
                return ret;
            }
            model.IsReceived = true;
            ret=dal.Update(model);
            return ret;
        }


        public Common.Result Delete(string ids)
        {
            var ret=dal.Delete(ids);
            return ret;
        }

        public Model.Chat Get(int id)
        {
            return dal.GetModel(id);
        }

        public IList<Model.Chat> Get(string where, string orderby, int start, int length, params object[] ps)
        {
            return dal.Get(where, orderby, start, length,ps);
        }

        public int GetCount(string where, params object[] ps)
        {
            return dal.GetCount(where, ps);
        }

    }
}
